package com.kgc.itrip.controller;

import com.kgc.itrip.common.DtoUtil;
import com.kgc.itrip.common.EmptyUtils;
import com.kgc.itrip.common.Page;
import com.kgc.itrip.dto.Dto;
import com.kgc.itrip.entity.ItripHotelVO;
import com.kgc.itrip.response.ResultCode;
import com.kgc.itrip.service.HotelSearchService;
import com.kgc.itrip.vo.hotel.SearchHotCityVO;
import com.kgc.itrip.vo.hotel.SearchHotelVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 酒店搜索的控制器
 *
 * */
@Slf4j
@Api(value = "酒店业务接口")
@RestController
@RequestMapping("/api/hotellist")
public class HotelListController {
    @Resource
    private HotelSearchService hotelSearchServicel;
    @ApiOperation(value = "查询酒店分页(用于查询酒店列表)",
            httpMethod = "POST",notes = "查询酒店分页(用于查询酒店列表)",
            protocols = "http",produces = "application/json;charset=UTF-8",
            consumes = "application.json")
    @PostMapping(value="/searchItripHotelPage")
    public Dto<Page<SearchHotelVO>> searchItripHotelPage(@RequestBody SearchHotelVO hotelVO) throws Exception {
        //判断传入的目的地是否为空
        if(EmptyUtils.isEmpty(hotelVO)||EmptyUtils.isEmpty(hotelVO.getDestination())){
            return DtoUtil.returnFail(ResultCode.DESTINATION_IS_EXISTS.getMsg(),ResultCode.DESTINATION_IS_EXISTS.getCode());
        }
        //调用业务逻辑层去执行搜索的操作
        Page<ItripHotelVO> page = null;
        try {
            page = hotelSearchServicel.searchHotelByCondition(hotelVO);
        }catch (Exception e){
            e.printStackTrace();
            log.error("从solr中查询酒店数据异常");
            return DtoUtil.returnFail(ResultCode.SYSTEM_IS_EXISTS.getMsg(),ResultCode.SYSTEM_IS_EXISTS.getCode());
        }
        return DtoUtil.returnDataSuccess(page);
    }

    @ApiOperation(value = "根据热门城市查询酒店",
            httpMethod = "POST",notes = "根据热门城市查询酒店",
            protocols = "http",produces = "application/json;charset=UTF-8",
            consumes = "application/json")
    @PostMapping(value="/searchItripHotelListByHotCity")
    public Dto<List<SearchHotCityVO>> searchItripHotelListByHotCity(@RequestBody SearchHotCityVO searchHotCityVO){
        if(EmptyUtils.isEmpty(searchHotCityVO) || EmptyUtils.isEmpty(searchHotCityVO.getCityId())){
            return DtoUtil.returnFail(ResultCode.CITY_IS_EXISTS.getMsg(),ResultCode.CITY_IS_EXISTS.getCode());
        }
        try {
            List<ItripHotelVO> itripHotelVOS = hotelSearchServicel.searchItripHotelListByHotCity(searchHotCityVO);
            return DtoUtil.returnDataSuccess(itripHotelVOS);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("solr查询数据异常");
            return DtoUtil.returnFail(ResultCode.SYSTEM_IS_EXISTS1.getMsg(),ResultCode.SYSTEM_IS_EXISTS1.getCode());
        }
    }
}
